<?php
/**
 * Created by PhpStorm.
 * User: kkw
 * Date: 2019/1/27
 * Time: 19:48
 */

namespace kkw\goods\response;
use kkw\user\Util;
use kkw\goods\table\GoodsTable;
use kkw\goods\table\GoodsImgTable;
use suda\core\Request;
use suda\core\Response;

class ReleaseGoods extends  Response
{
    public function onRequest(Request $request)
    {
        Util::printHeader();
        $goods_table = new GoodsTable();
        $json =  Request::json();
        $user_id = Util::getUserId();
        try {
            $goods_id = $goods_table->insert([
                'title' => $json['title'],
                'describe' => $json['describe'],
                    'goodstype'=> $json['goodstype'],
                    'mobile' => $json['mobile'],
                    'price' => $json['price'],
                    'qq' => $json['qq'],
                    'time' => time(),
                    'wechat' => $json['wechat'],
                    'address' => $json['address'],
                    'user_id' => $user_id
            ]);
            if($user_id = Util::getUserId()){
                //1、 -  取用户ID
                $user_id = Util::getUserId();
                // 2、根据该 $goods_Id 查找商品 img.good_id = 0   and userid = id;
                $goods_img = new GoodsImgTable();
                try {
                    $goods_img->update(['goods_id' => $goods_id], ['goods_id' =>0 , 'user_id' => $user_id]);
                } catch (\suda\exception\SQLException $e){
                }
            }
        } catch (\suda\exception\SQLException $e) {
            $this->json(['code' => $e->getCode(), 'message' => '添加二手商品失败']);
            return;
        }
        $this->json(['code' => 0,'message'=>'添加二手商品成功']);
    }
}